package genedit.gui.propertyDialogs;

import genedit.gui.GMainFrame;
import genedit.gui.exceptions.RequiredFieldException;
import genedit.model.graph.GraphElement;
import genedit.model.graph.classModel.AssociationLink;
import genedit.model.graph.classModel.ClassModel;
import genedit.model.graph.classModel.GeneralizationLink;
import genedit.model.graph.classModel.InterfaceModel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class PanelDetailGeneralizationLink   extends AbstractPanelDetail {
	
	private JLabel lblName;
	private JTextField txtName;
	
	private JButton btnStart;
	private JButton btnEnd;
	protected GeneralizationLink generalizationLinkEl;
	
	public PanelDetailGeneralizationLink(GraphElement element) {
		
		generalizationLinkEl = (GeneralizationLink)element;
		
		setLayout(new BorderLayout());

		Dimension lblDimension = new Dimension(50,20);
		
		Box boxCentar = new Box(BoxLayout.Y_AXIS);
		Box boxCentar1 = new Box(BoxLayout.Y_AXIS);
		Box boxCentar2 = new Box(BoxLayout.Y_AXIS);
		
		JPanel panName = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel panGroups = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblName = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblName"));
		lblName.setPreferredSize(lblDimension);
		txtName = new JTextField(10);
		txtName.setText(generalizationLinkEl.getName());
		panName.add(lblName);
		panName.add(txtName);
		
		boxCentar.add(panName);
		
		
		add(boxCentar,BorderLayout.CENTER);
		
	
		btnStart = new JButton(GMainFrame.getInstance().getResourceBundle().getString("child"));
		btnStart.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(generalizationLinkEl.getStart().getParent() instanceof ClassModel){
					ClassModelDialog dialog = new ClassModelDialog(GMainFrame.getInstance(), generalizationLinkEl.getStart().getParent());
					dialog.setVisible(true);
				}
				else if(generalizationLinkEl.getStart().getParent() instanceof InterfaceModel){
					InterfaceModelDialog dialog = new InterfaceModelDialog(GMainFrame.getInstance(), generalizationLinkEl.getStart().getParent());
					dialog.setVisible(true);
				}
				}
		});
		
		btnEnd = new JButton(GMainFrame.getInstance().getResourceBundle().getString("parent"));
		btnEnd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(generalizationLinkEl.getEnd().getParent() instanceof ClassModel){
					ClassModelDialog dialog = new ClassModelDialog(GMainFrame.getInstance(), generalizationLinkEl.getEnd().getParent());
					dialog.setVisible(true);
				}
				else if(generalizationLinkEl.getEnd().getParent() instanceof InterfaceModel){
					InterfaceModelDialog dialog = new InterfaceModelDialog(GMainFrame.getInstance(), generalizationLinkEl.getEnd().getParent());
					dialog.setVisible(true);
				}
			}
		});
		
		boxCentar1.add(btnStart);
		boxCentar2.add(btnEnd);

	
		
		boxCentar1.setAlignmentX(LEFT_ALIGNMENT);
		boxCentar2.setAlignmentX(LEFT_ALIGNMENT);
		
		panGroups.add(boxCentar1);
		panGroups.add(boxCentar2);
		
		boxCentar.add(panGroups);
		
		add(boxCentar,BorderLayout.CENTER);
		

	}
	




	

	public JTextField getTxtName() {
		return txtName;
	}





	public void setTxtName(JTextField txtName) {
		this.txtName = txtName;
	}





	public void proveriIspravnostPodataka() throws RequiredFieldException {
		
		if (txtName.getText().trim().equals("")) {
			txtName.setBackground(Color.RED);
		//	ok = false;
			txtName.addKeyListener(new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent e) {
					
					
				}
				
				@Override
				public void keyReleased(KeyEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.isActionKey() || e.getKeyCode()==KeyEvent.VK_ENTER || e.getKeyCode()==KeyEvent.VK_BACK_SPACE)
						return;
					
					else
						txtName.setBackground(Color.WHITE);
			//		ok = true;
					
				}
				
			});
			RequiredFieldException rfe = new RequiredFieldException(
					);
			throw rfe;
		}
		

	}


	

	}
